use diesel::prelude::*;

#[derive(Queryable, Selectable, serde::Serialize, Insertable, serde::Deserialize, Clone, Debug)]
#[diesel(table_name = crate::schema::user)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct User {
    pub id: i32,
    pub username: String,
    pub password: String,
    pub role: i32,
}

#[derive(
    Queryable,
    Selectable,
    serde::Serialize,
    Insertable,
    Associations,
    Clone,
    serde::Deserialize,
    Identifiable,
    Default,
)]
#[diesel(table_name = crate::schema::material)]
#[diesel(belongs_to(Contract))]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Material {
    pub id: i32,
    pub contract_id: i32,
    pub delivery_date: i64,
    pub delivery_numer: Option<String>,
    pub affiliation: Option<String>,
    pub category: Option<String>,
    pub name: String,
    pub specification: String,
    pub unit: Option<String>,
    pub verify_number: f64,
    pub price: f64,
    pub verify_amount: Option<f64>,
    pub comment: Option<String>,
    pub verify_date: Option<i64>,
    pub verified: Option<i32>,
    pub ext_name1: Option<String>,
    pub ext_name2: Option<String>,
    pub ext_name3: Option<String>,
    pub ext_name4: Option<String>,
    pub ext_name5: Option<String>,
    pub ext_name6: Option<String>,
    pub ext_name7: Option<String>,
    pub ext_name8: Option<String>,
    pub ext_name9: Option<String>,
    pub ext_name10: Option<String>,
    pub ext_name11: Option<String>,
    pub ext_name12: Option<String>,
    pub ext_name13: Option<String>,
    pub ext_name14: Option<String>,
    pub ext_name15: Option<String>,
    pub ext_name16: Option<String>,
    pub ext_name17: Option<String>,
    pub ext_name18: Option<String>,
    pub ext_name19: Option<String>,
    pub ext_name20: Option<String>,
    pub ext_value1: Option<String>,
    pub ext_value2: Option<String>,
    pub ext_value3: Option<String>,
    pub ext_value4: Option<String>,
    pub ext_value5: Option<String>,
    pub ext_value6: Option<String>,
    pub ext_value7: Option<String>,
    pub ext_value8: Option<String>,
    pub ext_value9: Option<String>,
    pub ext_value10: Option<String>,
    pub ext_value11: Option<String>,
    pub ext_value12: Option<String>,
    pub ext_value13: Option<String>,
    pub ext_value14: Option<String>,
    pub ext_value15: Option<String>,
    pub ext_value16: Option<String>,
    pub ext_value17: Option<String>,
    pub ext_value18: Option<String>,
    pub ext_value19: Option<String>,
    pub ext_value20: Option<String>,
    pub his_defaulters_amount: Option<f64>,
    pub his_hang_invoice_amount: Option<f64>,
    pub his_flag: Option<i32>,
}

#[derive(
    Queryable, Selectable, Insertable, Identifiable, serde::Serialize, Clone, serde::Deserialize,
)]
#[diesel(table_name = crate::schema::project)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Project {
    pub id: i32,
    pub name: String,
    pub remarks: Option<String>,
    pub deadline_time: Option<i64>,
}

#[derive(
    Queryable, Selectable, Insertable, Identifiable, serde::Serialize, Clone, serde::Deserialize,
)]
#[diesel(table_name = crate::schema::provider)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Provider {
    pub id: i32,
    pub name: String,
    pub contact_name: String,
    pub contact_phone: String,
    pub remarks: Option<String>,
}

#[derive(
    Queryable,
    Selectable,
    Insertable,
    Identifiable,
    serde::Serialize,
    Clone,
    serde::Deserialize,
    Debug,
)]
#[diesel(table_name = crate::schema::contract)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
#[diesel(belongs_to(Project))]
#[diesel(belongs_to(Provider))]
pub struct Contract {
    pub id: i32,
    pub project_id: i32,
    pub provider_id: i32,
    pub no: String,
    pub sub_contract: i32,
    pub parent_no: Option<String>,
    pub contract_date: Option<i64>,
    pub contract_amount: f64,
    pub remarks: Option<String>,
}

#[derive(
    Queryable,
    Selectable,
    Insertable,
    Identifiable,
    serde::Serialize,
    Clone,
    serde::Deserialize,
    Debug,
)]
#[diesel(table_name = crate::schema::payment)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
#[diesel(belongs_to(Contract))]
pub struct Payment {
    pub id: i32,
    pub contract_id: i32,
    pub pay_amount: f64,
    pub pay_date: i64,
    pub payer: String,
    pub special: Vec<u8>,
    pub special_payment: Option<String>,
    pub special_amount: Option<f64>,
    pub remarks: Option<String>,
}

#[derive(
    Queryable,
    Selectable,
    Insertable,
    Identifiable,
    serde::Serialize,
    Clone,
    serde::Deserialize,
    Debug,
)]
#[diesel(table_name = crate::schema::invoice)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
#[diesel(belongs_to(Contract))]
pub struct Invoice {
    pub id: i32,
    pub contract_id: i32,
    pub amount: f64,
    pub invoice_date: i64,
    pub remarks: Option<String>,
}
